package undoredo;

/**
 * This class contain all command actions to be store in stack
 */
import java.util.Stack;

public class CommandStack extends AbstractCommand
{
   @SuppressWarnings("unused")
   private AbstractCommand command;
   private Stack<AbstractCommand> redo = new Stack<AbstractCommand>();
   private Stack<AbstractCommand> undo = new Stack<AbstractCommand>();

   public CommandStack()
   {
   }

   /**
    * This method take the command and add to the stack
    * 
    * @param command
    *           undo redu comnnad such, addElement moving shape etc
    */
   public void addToStack(AbstractCommand command)
   {
      // TODO Auto-generated method stub
      this.command = command;
      undo.push(command);
      redo = null;
      redo = new Stack<AbstractCommand>();
   }

   public boolean checkRedo()
   {
      if (redo.isEmpty())
         return false;
      else
         return true;
   }

   public boolean checkUndo()
   {
      if (undo.isEmpty())
         return false;
      else
         return true;
   }

   @Override
   public void execute()
   {
      // TODO Auto-generated method stub

   }

   public Stack<AbstractCommand> getRedo()
   {
      return redo;
   }

   public Stack<AbstractCommand> getUndo()
   {
      return undo;
   }

   /*
    * This redo command is to reload to the stack
    */
   public void redo()
   {
      if (!redo.isEmpty())
      {
         AbstractCommand tmp = redo.pop();
         undo.push(tmp);
         tmp.execute();
      }
   }

   @Override
   public void undo()
   {
      if (!undo.isEmpty())
      {
         AbstractCommand tmp = undo.pop();
         redo.push(tmp);
         tmp.undo();
      }
   }
}
